package com.winning.sx.microframework.plugins.wx;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.dao.EasyDao;
import com.winning.sx.microframework.dao.sysmanage.GenericApiManageDao;
import com.winning.sx.microframework.model.genericapi.GenericApiInterfaceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信配置类
 *
 * @author lst
 */
@Service
public class WxConfig {

    @Autowired
    private EasyDao easyDao;
    @Autowired
    private GenericApiManageDao genericApiManageDao;
    public static String appid = "wx15e86d161a4b2da8";//获取微信公众号 id
    public static String appSecret = "9e6128546122957f503a0659eaeb8eae";//微信秘钥
    public static String sign_type = "MD5";//加密类型
    public static String CNY = "CNY";//人民币类型
    public static String mch_id = "1513093641";//商户号id
    public static String key = "sxsrmyy20180831PUBLICdeveloperst";
    public static String apiclientCert = "D:/wx/secret/apiclient_cert.p12";

    public static boolean validCodeTimeOut=false;

    public static String mch_id2 = "1530923441";//商户号id
    public static String key2 = "CkiiJ2v7J5LJnhcElVgGzlGkebI49ZNj";
    public static String apiclientCert2 = "D:/wx/secret/apiclient_cert2.p12";


    public static String accessToken="55_3Fpo1IFDuLs_pqpgKMwqORNmf7bAXQepqlpdw18BUKz-1Wv1FUJt__LEG7NNvKPHca1HhYcDXX8VrAlo5yfSBPtimENZ3K_fS28xuHXYDWKg1LCO0YySPb_0tIH3dyqadInLSNf0YicvpIWuYSCaAGAMGQ";
    public static String jsapiTicket="";


    public static String notifyUrl = "http://www.sxsrmyywx.com/wechat/pay/notifyUrl";
    public static String refundNotifyUrl = "http://www.sxsrmyywx.com/wechat/pay/refundNotifyUrl";

    public static String refundQuery = "https://api.mch.weixin.qq.com/pay/refundquery";//退款查询
    public static String unifiedorder = "https://api.mch.weixin.qq.com/pay/unifiedorder";//统一下单
    public static String querydorder = "https://api.mch.weixin.qq.com/pay/orderquery";//查詢支付状态
    public static String refundorder = "https://api.mch.weixin.qq.com/secapi/pay/refund";//退款
    public static String sendTemplateMessage="https://api.weixin.qq.com/cgi-bin/message/template/send";//发送模板消息

    public static boolean runState = true;
    public static String baseUrl;
    public static String logFile;
    public static String id;
    public static Map<String, Object> api = new HashMap();

    //启动后运行
    @PostConstruct
    public void init() {
        try {
            System.out.println("启动 微信服务器配置");
            initServer();
            initServerConfig();
            initApiConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean initServer() {
        try {
            Map param = new HashMap();
            param.put("code", "wx");
            Map server = easyDao.queryEntity("SYS_ServerInfo", param);
            WxConfig.baseUrl = "https://" + server.get("domainName").toString() + "/" + server.get("virtualRoute").toString();
            WxConfig.logFile = server.get("logAddress").toString();
            WxConfig.id = server.get("id").toString();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean initServerConfig() {
        try {
            Map param = new HashMap();
            param.put("serverId", WxConfig.id.toString());
            param.put("state", Constains.STATE_NORMAL);
            DataTable configTable = easyDao.queryList("SYS_ServerConfig", param);
            List config = configTable.getDataRows();
            for (int i = 0; i < config.size(); i++) {
                Map data = (Map) config.get(i);
                switch (data.get("code").toString()) {
                    case "appid":
                        WxConfig.appid = data.get("value").toString();
                        break;
                    case "appSecret":
                        WxConfig.appSecret = data.get("value").toString();
                        break;
                    case "sign_type":
                        WxConfig.sign_type = data.get("value").toString();
                        break;
                    case "runState":
                        WxConfig.runState = Constains.STATE_NORMAL.equals(data.get("value").toString());
                        break;
                    case "mch_id":
                        WxConfig.mch_id = data.get("value").toString();
                        break;
                    case "key":
                        WxConfig.key = data.get("value").toString();
                        break;
                    case "apiclientCert":
                        WxConfig.apiclientCert = data.get("value").toString();
                        break;
                    case "mch_id2":
                        WxConfig.mch_id2 = data.get("value").toString();
                        break;
                    case "key2":
                        WxConfig.key2 = data.get("value").toString();
                        break;
                    case "apiclientCert2":
                        WxConfig.apiclientCert2 = data.get("value").toString();
                        break;
                    case "validCodeTimeOut":
                        WxConfig.validCodeTimeOut = Boolean.parseBoolean(data.get("value").toString());
                        break;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean initApiConfig() {
        try {
            Map param = new HashMap();
            param.put("serverId", WxConfig.id.toString());
            param.put("state", Constains.STATE_NORMAL);
            DataTable apiTable = easyDao.queryList("SYS_InterfaceInfo", param);
            List api = apiTable.getDataRows();
            WxConfig.api = new HashMap();
            for (int i = 0; i < api.size(); i++) {
                Map data = (Map) api.get(i);

                List<GenericApiInterfaceConfig> propties = genericApiManageDao.queryInterfacePropties(Integer.parseInt(data.get("id").toString()));
                WxConfig.api.put(data.get("code").toString(), propties);
                WxConfig.api.put(data.get("code").toString() + "Config", data);
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

}
